Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
@travetto/worker
Advanced tools
Process management utilities, with a focus on inter-process communication
Install: @travetto/worker
npm install @travetto/worker
# or
yarn add @travetto/worker
This module provides the necessary primitives for handling dependent workers. A worker can be an individual actor or could be a pool of workers. Node provides ipc (inter-process communication) functionality out of the box. This module builds upon that by providing enhanced event management, richer process management, as well as constructs for orchestrating a conversation between two processes.
With respect to managing multiple executions, WorkPool is provided to allow for concurrent operation, and processing of jobs concurrently. To manage the flow of jobs, there are various WorkSet implementation that allow for a wide range of use cases.
The only provided WorkSet is the IterableWorkSet which supports all Iterable
and Iterator
sources. Additionally, the module provides ManualAsyncIterator which allows for manual control of iteration, which is useful for event driven work loads.
Below is a pool that will convert images on demand, while queuing as needed.
Code: Image processing queue, with a fixed batch/pool size
import { ExecUtil, ExecutionState } from '@travetto/base';
import { Worker, WorkPool, IterableWorkSet, ManualAsyncIterator } from '@travetto/worker';
class ImageProcessor implements Worker<string> {
active = false;
proc: ExecutionState;
get id(): number | undefined {
return this.proc.process.pid;
}
async destroy(): Promise<void> {
this.proc.process.kill();
}
async execute(path: string): Promise<void> {
this.active = true;
try {
this.proc = ExecUtil.spawn('convert images', [path]);
await this.proc;
} catch {
// Do nothing
}
this.active = false;
}
}
export class ImageCompressor extends WorkPool<string> {
pendingImages = new ManualAsyncIterator<string>();
constructor() {
super(async () => new ImageProcessor());
}
begin(): void {
this.process(new IterableWorkSet(this.pendingImages));
}
convert(...images: string[]): void {
this.pendingImages.add(images);
}
}
Once a pool is constructed, it can be shutdown by calling the .shutdown()
method, and awaiting the result.
Within the comm
package, there is support for two primary communication elements: ChildCommChannel and ParentCommChannel. Usually ParentCommChannel indicates it is the owner of the sub process. ChildCommChannel indicates that it has been created/spawned/forked by the parent and will communicate back to it's parent. This generally means that a ParentCommChannel can be destroyed (i.e. killing the subprocess) where a ChildCommChannel can only exit the process, but the channel cannot be destroyed.
A common pattern is to want to model a sub process as a worker, to be a valid candidate in a WorkPool. The WorkUtil class provides a utility to facilitate this desire.
Code: Spawned Worker
import { ExecutionState } from '@travetto/base';
import { ParentCommChannel } from './comm/parent';
import { Worker } from './pool';
type Simple<V> = (ch: ParentCommChannel<V>) => Promise<unknown | void>;
type Param<V, X> = (ch: ParentCommChannel<V>, input: X) => Promise<unknown | void>;
const empty = async (): Promise<void> => { };
/**
* Spawned worker
*/
export class WorkUtil {
/**
* Create a process channel worker from a given spawn config
*/
static spawnedWorker<V, X>(
worker: () => ExecutionState,
init: Simple<V>,
execute: Param<V, X>,
destroy: Simple<V> = empty): Worker<X> {
const channel = new ParentCommChannel<V>(worker());
return {
get id(): number | undefined { return channel.id; },
get active(): boolean { return channel.active; },
init: () => init(channel),
execute: inp => execute(channel, inp),
async destroy(): Promise<void> {
await destroy(channel);
await channel.destroy();
},
};
}
}
When creating your work, via process spawning, you will need to provide the script (and any other features you would like in SpawnConfig
). Additionally you must, at a minimum, provide functionality to run whenever an input element is up for grabs in the input source. This method will be provided the communication channel (ParentCommChannel) and the input value. A simple example could look like:
Code: Spawning Pool
import { ExecUtil } from '@travetto/base';
import { WorkPool, WorkUtil, IterableWorkSet } from '@travetto/worker';
export async function main(): Promise<void> {
const pool = new WorkPool(() =>
WorkUtil.spawnedWorker<{ data: number }, number>(
() => ExecUtil.spawn('trv', ['main', '@travetto/worker/doc/spawned.ts']),
ch => ch.once('ready'), // Wait for child to indicate it is ready
async (channel, inp) => {
const res = channel.once('response'); // Register response listener
channel.send('request', { data: inp }); // Send request
const { data } = await res; // Get answer
console.log('Request complete', { input: inp, output: data });
if (!(inp + inp === data)) {
// Ensure the answer is double the input
throw new Error(`Did not get the double: inp=${inp}, data=${data}`);
}
}
)
);
await pool.process(new IterableWorkSet([1, 2, 3, 4, 5]));
}
Code: Spawned Worker
import timers from 'timers/promises';
import { ChildCommChannel } from '@travetto/worker';
export async function main(): Promise<void> {
const exec = new ChildCommChannel<{ data: string }>();
exec.on('request', data =>
exec.send('response', { data: (data.data + data.data) })); // When data is received, return double
exec.send('ready'); // Indicate the child is ready to receive requests
for await (const _ of timers.setInterval(5000)) {
// Keep-alive
}
}
Terminal: Output
$ trv main doc/spawner.ts
Request complete { input: 1, output: 2 }
Request complete { input: 2, output: 4 }
Request complete { input: 3, output: 6 }
Request complete { input: 4, output: 8 }
Request complete { input: 5, output: 10 }
FAQs
Process management utilities, with a focus on inter-process communication
The npm package @travetto/worker receives a total of 10 weekly downloads. As such, @travetto/worker popularity was classified as not popular.
We found that @travetto/worker demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.